'use strict'

import Mock from 'mockjs';
import Config from './Config';
import QueryString from 'query-string';
import Lodash from 'lodash';

let Request = {}

Request.get = (url, params) => {
    // 将params与url合并用 query-string
    if (params) {
        url += '?' + QueryString.stringify(params)
    }

    return fetch(url)
            .then((response) => response.json())
            .then((responseData) => {
                return Mock.mock(responseData)
            })
            .catch((error) => {
                console.log(error);
            })
}

Request.post = (url, body) => {
    // 将body与url合并用 lodash
    let map = Lodash.extend(Config.map, {
        body: JSON.stringify(body)
    })

    return fetch(url, map)
            .then((response) => response.json())
            .then((responseData) => Mock.mock(responseData))
            .catch((error) => console.log(error))
}

module.exports = Request;
